package kit.docker.openWebsocketServer;

import com.docker.core.mvc.Remark;
import kit.docker.datetime.DateKit;
import kit.docker.security.MD5Kit;
import kit.docker.web.RequestKit;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;

import java.util.Map;

/**
 * @Author ldl
 * @Create 2020/08/19 019 14:54
 */
@Remark("调用websocket的http连接")
public class WebsocketHttpKit extends Thread {

    private static String omonAppID = "da9c1e2d82174015bd93bf5141fbfe70";
    private static String omonAppSecret = "d4e07e3ff9c14b50ab576a7aa6d42b2f";
    private static String omonUrl = "/api/websocket/omon";

    private String cmd;
    private Map<String, String> data;

    public WebsocketHttpKit(String cmd, Map<String, String> data) {
        this.cmd = cmd;
        this.data = data;
    }

    public void run() {
        post(this.cmd, this.data);
    }

    private void post(String cmd, Map<String, String> data) {
        try {
            HttpKit.post(PropKit.get("websocket.server") + omonUrl, buildData(cmd, data), null);
        } catch (Exception e) {
            System.out.println("websocket服务请求失败：" + e.getMessage());
        }
    }

    /**
     * @Param: [data 请求数据, cmd 命令码]
     * @return: 请求数据
     * @Author: ldl
     * @Create: 2020/08/19 019 17:37
     */
    @Remark("封装请求公用参数")
    private Map<String, String> buildData(String cmd, Map<String, String> data) {
        data.put("cmd", cmd);
        data.put("app_id", omonAppID);
        data.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        data.put("push_time", DateKit.getNowDateTime());

        String paramStr = RequestKit.formatParamMapToString(data);
        data.put("sign", MD5Kit.encodeMD5Hex(paramStr + omonAppSecret));

        return data;
    }

}
